/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jsf.generator.controller;

import net.db.generator.JavaBaseClassWriter;
import net.db.generator.utils.ClassGeneratorUtil;

/**
 *
 * @author abosancic
 */
public class JSFPaginationManagerWriter extends JavaBaseClassWriter {

    public JSFPaginationManagerWriter() {
        super();
    }

    @Override
    protected void generateClassName() {
        stringClass.append("public abstract class ").append(ClassGeneratorUtil.humanize(getClassName())).append(getExtendsClassName()).append(getImplementsInterfaceName()).append(" {\n\n");
        generateJsfUtilClass();
    }

    //==========================================================================
    // setters and getters
    //==========================================================================
    public void generateJsfUtilClass() {

        String daoClass = "    private int pageSize;\n"
                + "    private int page;\n"
                + "\n"
                + "    public PaginationManager(int pageSize) {\n"
                + "        this.pageSize = pageSize;\n"
                + "    }\n"
                + "\n"
                + "    public abstract int getItemsCount();\n"
                + "\n"
                + "    public abstract DataModel createPageDataModel();\n"
                + "\n"
                + "    public int getPageFirstItem() {\n"
                + "        return page * pageSize;\n"
                + "    }\n"
                + "\n"
                + "    public int getPageLastItem() {\n"
                + "        int i = getPageFirstItem() + pageSize - 1;\n"
                + "        int count = getItemsCount() - 1;\n"
                + "        if (i > count) {\n"
                + "            i = count;\n"
                + "        }\n"
                + "        if (i < 0) {\n"
                + "            i = 0;\n"
                + "        }\n"
                + "        return i;\n"
                + "    }\n"
                + "\n"
                + "    public boolean isHasNextPage() {\n"
                + "        return (page + 1) * pageSize + 1 <= getItemsCount();\n"
                + "    }\n"
                + "\n"
                + "    public void nextPage() {\n"
                + "        if (isHasNextPage()) {\n"
                + "            page++;\n"
                + "        }\n"
                + "    }\n"
                + "\n"
                + "    public boolean isHasPreviousPage() {\n"
                + "        return page > 0;\n"
                + "    }\n"
                + "\n"
                + "    public void previousPage() {\n"
                + "        if (isHasPreviousPage()) {\n"
                + "            page--;\n"
                + "        }\n"
                + "    }\n"
                + "\n"
                + "    public int getPageSize() {\n"
                + "        return pageSize;\n"
                + "    }\n";
        stringClass.append(daoClass);
    }

    @Override
    protected void generateFields() {
    }

    @Override
    protected void generateToString() {
    }

    @Override
    protected void generateHashCode() {
    }

    @Override
    protected void generateEquals() {
    }

    @Override
    protected void generateConstructor() {
    }

    @Override
    protected void generateGettersAndSetters() {
    }
}
